package com.ykt.model;

import com.jfinal.plugin.activerecord.Page;
import com.ykt.common.utils.PageUtil;
import com.ykt.common.utils.StringUtil;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 社区
 * @author wing
 * @version 1.0.0
 */
public class Community extends BaseModel<Community> {
    private static final long serialVersionUID = 1L;

    public static final String address = "address";
    public static final String longitude = "longitude";
    public static final String latitude = "latitude";
    public static final String contacts_name = "contacts_name";
    public static final String contacts_phone = "contacts_phone";

    public static final Community dao = new Community();

    public List<Community> findAll(){
    	String sql = "SELECT c.* FROM community c ";
		return find(sql);
    }

    public List<Community> findByShopId(int shopId) {
        String sql = "SELECT c.* FROM community c, community_shop cs WHERE c.id = cs.community_id AND c.status=1 AND cs.shop_id=?";
        return this.find(sql, shopId);
    }

    public PageData<Community> paginate4Cms(PageParam<Map<String, Object>> param) {
        String select = "SELECT c.* ";
        String exceptSelect = " FROM community c ";

        String paramsSql = " WHERE ";
        List<Object> paramsVal = new ArrayList<>();
        for (Map.Entry<String, Object> entry : param.getP().entrySet()) {
            if (entry.getValue() != null) {
                paramsVal.add(entry.getValue());
                paramsSql += entry.getKey() + "= ? and ";
            }
        }

        Page<Community> page = null;
        if (paramsVal.isEmpty()) {
            page = this.paginate(param.getPageNo(), param.getPageSize(), select, exceptSelect);
        } else {
            exceptSelect = exceptSelect + StringUtil.trimRight(paramsSql, "and ");
            page = this.paginate(param.getPageNo(), param.getPageSize(), select, exceptSelect, paramsVal.toArray());
        }

        return PageUtil.to(page);
    }
}
